<!DOCTYPE html>
<html>
<head>
    <title>设置分类</title>
    {% include 'system/common/header.html' %}
</head>
<body class="pear-container">
    <!-- 数据表格 -->
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="category-table" lay-filter="category-table"></table>
        </div>
    </div>

    <!-- 表格工具栏 -->
    <script type="text/html" id="category-toolbar">
        {% if authorize("system:iptvadmin:add") %}
        <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>新增
        </button>
        {% endif %}
        {% if authorize("system:iptvadmin:remove") %}
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="batchRemove">
            <i class="layui-icon layui-icon-delete"></i>删除
        </button>
        {% endif %}
    </script>

    <!-- 行工具栏 -->
    <script type="text/html" id="category-bar">
        {% if authorize("system:iptvadmin:edit") %}
        <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit">
            <i class="layui-icon layui-icon-edit"></i>
        </button>
        {% endif %}
        {% if authorize("system:iptvadmin:remove") %}
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">
            <i class="layui-icon layui-icon-delete"></i>
        </button>
        {% endif %}
    </script>

    {% include 'system/common/footer.html' %}
    <script>
        layui.use(['table', 'form', 'jquery'], function() {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;
    
            let MODULE_PATH = "/iptvadmin/";
    
            // 渲染表格
            table.render({
                elem: '#category-table',
                url: MODULE_PATH + 'category/query',
                page: true,
                cols: [[
                    {type: 'checkbox'},
                    {field: 'id', title: 'ID', sort: true},
                    {field: 'name', title: '分类名称'},
                    {field: 'sort', title: '排序', sort: true, edit: 'text'},
                    {field: 'create_time', title: '创建时间'},
                    {title: '操作', toolbar: '#category-bar', align: 'center', width: 130}
                ]],
                skin: 'line',
                toolbar: '#category-toolbar',
                defaultToolbar: [{
                    layEvent: 'refresh',
                    title: '刷新',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
            });

            // 工具栏事件
            table.on('toolbar(category-table)', function(obj) {
                if (obj.event === 'add') {
                    window.add();
                } else if (obj.event === 'refresh') {
                    window.refresh();
                } else if (obj.event === 'batchRemove') {
                    window.batchRemove(obj);
                }
            });

            // 行工具栏事件
            table.on('tool(category-table)', function(obj) {
                if (obj.event === 'remove') {
                    window.remove(obj);
                } else if (obj.event === 'edit') {
                    window.edit(obj);
                }
            });
    
            // 新增分类
            window.add = function() {
                layer.open({
                    type: 2,
                    title: '新增分类',
                    shade: 0.1,
                    area: ['500px', '300px'],
                    content: '/iptvadmin/category/add',
                });
            }
    
            // 编辑分类
            window.edit = function(obj) {
                layer.open({
                    type: 2,
                    title: '编辑分类',
                    shade: 0.1,
                    area: ['500px', '300px'],
                    content: MODULE_PATH + 'category/edit?id=' + obj.data.id,
                });
            }
    
            // 删除分类
            window.remove = function(obj) {
                layer.confirm('确定要删除该分类吗？', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: MODULE_PATH + 'category/remove',
                        data: {id: obj.data.id},
                        type: 'post',
                        success: function(result) {
                            layer.close(loading);
                            if (result.success) {
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function() {
                                    obj.del();
                                });
                            } else {
                                layer.msg(result.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    });
                });
            }

            // 批量删除分类
            window.batchRemove = function(obj) {
                let checkStatus = table.checkStatus('category-table');
                let selectedData = checkStatus.data;
                let ids = selectedData.map(item => item.id);
                
                if (ids.length === 0) {
                    layer.msg("请选择要删除的数据", {
                        icon: 2,
                        time: 1000
                    });
                    return false;
                }
                
                layer.confirm(`确定要删除选中的 ${ids.length} 条数据吗？`, {
                    icon: 3,
                    title: '警告',
                    btn: ['确定','取消']
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    
                    $.ajax({
                        url: MODULE_PATH + 'category/batchRemove',
                        data: {ids: ids.join(',')},
                        type: 'post',
                        success: function(result) {
                            layer.close(loading);
                            if (result.success) {
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function() {
                                    table.reload('category-table');
                                });
                            } else {
                                layer.msg(result.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        },
                        error: function() {
                            layer.close(loading);
                            layer.msg("服务器错误，请稍后重试", {
                                icon: 2,
                                time: 1000
                            });
                        }
                    });
                });
            }

            // 监听单元格编辑
            table.on('edit(category-table)', function(obj){
                let value = obj.value,
                    data = obj.data;
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + 'category/update',
                    data: JSON.stringify({
                        id: data.id,
                        sort: value
                    }),
                    contentType: 'application/json',
                    type: 'put',
                    success: function(result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            });
                            table.reload('category-table');
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                });
            });
        });
    </script>
</body>
</html>